package com.zqx.spseth.service.impl;

import com.zqx.spseth.dao.UserRepository;
import com.zqx.spseth.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class CustomUserServiceImpl implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public User loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userRepository.findByUsername(username);

        if(user == null)
        {
            throw new UsernameNotFoundException("用户名不存在");
        }else if("locked".equals(user.getStatus()))
        {
            throw new LockedException("用户被锁定");
        }
        return user;
    }
}
